<template>
  <div class="m-content">
    <h3>欢迎来到鹿存岿的博客！</h3>
    <div class="block">
      <el-avatar :size="50" :src="user.avatar"></el-avatar>
      <div>{{user.username}}</div>
    </div>
    <div class="maction">
      <span><el-link @click="main">主页</el-link>
      <el-divider direction="vertical"></el-divider>
      </span>
      <span><el-link @click="add" type="success">发表博客</el-link>
      <el-divider direction="vertical"></el-divider>
      </span>
      <span v-if="!hasLogin"><el-link @click="login" type="success">登录</el-link>
      <el-divider direction="vertical"></el-divider>
      </span>
      <span v-if="hasLogin"><el-link type="danger" @click="logout">退出</el-link></span>
    </div>
  </div>
</template>

<script>
 export default{
   name:"Header",
   data(){
     return{
       user: {
         username:'请先登录',
         avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
       },
       hasLogin:false
     }
   },
   methods:{
     logout(){
       this.$axios.get('/logout',{
         headers:{
           "Authorization":this.$store.state.token
         }
       }).then(res=>{
         this.$store.commit("SET_REMOVE")
         this.$router.push('/login')
       })
     },
     add(){
       this.$router.push('/blog/add')
     },
     main(){
       this.$router.push('/').catch(err => err)
     },
     login(){
       this.$router.push('/login')
     }
   },
   created() {
     if (this.$store.getters.getUser.username){
       this.user.username = this.$store.getters.getUser.username
       this.user.avatar = this.$store.getters.getUser.avatar
       this.hasLogin = true
     }
   }
 }
</script>

<style scoped>
.maction{
  margin: 10px 0;
}
.m-content{
  max-width: 960px;
  margin: 0 auto;
  text-align: center;
}
</style>
